<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class DbAction extends AdminAction {

    #列表
    public function index(){        

        $FileArr = $this->MyScandir(C('DB_DIR'));

        $list = array();
        foreach ($FileArr as $key => $value) {
            if ($key>1) {
                $list[$key]['name']=$value;
                $list[$key]['createTime']=date('Y-m-d H:i:s',filemtime(C('DB_DIR').$value));
                $FileSize = filesize(C('DB_DIR').$value)/1024;
                if ($FileSize < 1024){
                    $list[$key]['size'] = number_format($FileSize,2) . ' KB';
                } else {
                    $list[$key]['size'] = '<font color="#FF0000">' . number_format($FileSize/1024,2) . '</font> MB';
                }
            }
        }

        $this->assign('list',$list);

        $this->display();
    }

    protected function MyScandir($FilePath='./',$Order=0){
      $FilePath = opendir($FilePath);
      while (false !== ($filename = readdir($FilePath))) {
        $FileAndFolderAyy[] = $filename;
      }
      $Order == 0 ? sort($FileAndFolderAyy) : rsort($FileAndFolderAyy);
      return $FileAndFolderAyy;
    }

    #备份
    public function backup(){
        import("@.ORG.MySQLReback");
        $config = array(
            'host' => C('DB_HOST'),
            'port' => C('DB_PORT'),
            'userName' => C('DB_USER'),
            'userPassword' => C('DB_PWD'),
            'dbprefix' => C('DB_PREFIX'),
            'charset' => 'UTF8',
            'path' => C('DB_DIR'),
            'isCompress' => 0, //是否开启gzip压缩
            'isDownload' => 0  
        );
        $mr = new MySQLReback($config);
        $mr->setDBName(C('DB_NAME'));
        $mr->backup();                
        $this->success( '数据库备份成功！');
    }

    #还原
    public function restore(){
        import("@.ORG.MySQLReback");
        $config = array(
            'host' => C('DB_HOST'),
            'port' => C('DB_PORT'),
            'userName' => C('DB_USER'),
            'userPassword' => C('DB_PWD'),
            'dbprefix' => C('DB_PREFIX'),
            'charset' => 'UTF8',
            'path' => C('DB_DIR'),
            'isCompress' => 0, //是否开启gzip压缩
            'isDownload' => 0  
        );
        $mr = new MySQLReback($config);
        $mr->setDBName(C('DB_NAME'));
        $mr->recover(I('get.File'));                
        $this->success( '数据库还原成功！');
    }

    #下载
    public function down(){
        function DownloadFile($fileName) {
            ob_end_clean();
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Length: ' . filesize($fileName));
            header('Content-Disposition: attachment; filename=' . basename($fileName));
            readfile($fileName);
        }
        $file = C('DB_DIR').I('get.File');
        DownloadFile($file);
        exit();
    }

    #删除
    public function del(){
        $file = C('DB_DIR').I('get.File');
        if (@unlink($file)) {
            $this->success('删除成功！');
        } else {                    
            $this->error('删除失败！');
        }
    }     

}